require.config({baseUrl: '/js/lib', urlArgs: "v0.0.3"});
require(['zepto', 'lib', 'uploadImg'], function ($, lib) {
    var fn = {}, lib = new lib();
    $.extend(fn, {
        init: function () {
            fn.getRestaurantsInfo();
            fn.getRecommend();
            $(document).on('tap click', '.ui-per-goods', function () {
                location.href = '/wine_shop.html?sId=' + $(this).data('sid');
            })
        },
        getRestaurantsInfo: function () {
            var param = {
                'lon': lib.getUrlParam('lon'),
                'lat': lib.getUrlParam('lat')
            };
            lib.ajx(lib.getReq().ser + 'storage/searchCoord.action', param, function (data) {
                    if (data.infocode == '0') {
                        var info = data.info;
                        var html = '';
                        for (var i in info) {
                            var distance = info[i].distance.toString();
                            if (distance > 999) {
                                distance = (distance.substring(0, distance.indexOf('.')) / 1000);
                                var offset = 0;
                                if (distance > 10 && distance < 100) {
                                    offset = 1;
                                } else if (distance > 100 && distance < 1000) {
                                    offset = 0;
                                } else if (distance > 1000 && distance < 10000) {
                                    offset = -1;
                                } else if (distance > 10000 && distance < 100000) {
                                    offset = -2;
                                }
                                distance = distance.toString().substring(0, 3 - offset) + 'Km'
                            } else {
                                distance = distance.substring(0, distance.indexOf('.')) + 'm';
                            }
                            html += `<li class="ui-per-goods" data-sid="${info[i].storageId}">
                                        <ul>
                                          <li><img src="${lib.getReq().imgPath + ( info[i].imgUrl.split(',')[0] )}" ></li>
                                          <li>
                                            <p>${info[i].storageName}</p>
                                            <p>${info[i].address}</p>
                                            <p><span>城市酒柜</span><span>${distance}</span></p>
                                          </li>
                                        </ul>
                                      </li>`;
                        }
                        $('#restaurants').html(html);
                        $('.ui-per-goods').on('tap', function () {
                            //TODO +跳转链接
                        });
                    } else {
                        alert('服务器开小差了，请稍后再试。')
                    }
                },
                function () {
                    console.log('nearby_city_wine.js => searchCoord.action fail!');
                }
            )
        },
        getRecommend: function () {
            var param = {
                'recommendId': '1',
                'pageNo': '1',
                'pageSize': '1'
            };
            lib.ajx(lib.getReq().ser + 'recommendLocation/getRecommendInfo.action', param, function (data) {
                    if (data.infocode == '0') {
                        var info = data.info.recommendGoods[0];
                        $('#recommendImg').attr('src', lib.getReq().imgPath + info.mainPictureJpg);
                        $('#recommendName').attr('href', info.recomendLink).html(info.chName);
                    } else {
                        alert('服务器开小差了，请稍后再试。')
                    }
                },
                function () {
                    console.log('nearby_city_wine.js => getRecommendInfo.action fail!');
                }
            )
        }
    });
    fn.init();
});